# -*- coding: utf-8 -*-

__author__ = 'bilibili'

import threading
from spot_AM_Report_analyzer import spot_AM_Report_analyzer

class return_data:
    # FUNCTION: __init__(self)
    #   the constructor of the class return_data
    def __init__(self):
        self.below_50 = 0
        self.between_50_and_70 = 0
        self.between_70_and_90 = 0
        self.between_90_and_100 = 0
        self.more_than_100 = 0
        self.total = 0

    # FUNCTION: to_dict(self)
    #   to make the content into one dictionary, so it's easy to write into a json format
    # RETURN:
    #   dict: the dictionary that contains all the class variables' information
    def to_dict(self):
        dict = {}
        dict['below_50'] = self.below_50
        dict['between_50_to_70'] = self.between_50_and_70
        dict['between_70_to_90'] = self.between_70_and_90
        dict['between_90_to_100'] = self.between_90_and_100
        dict['more_than_100'] = self.more_than_100
        dict['total'] = self.total
        return dict

class spot_returner:
    '''
    SINGLETON PATTERN
    '''
    instance = None  
    mutex = threading.Lock()

    def __init__(self):
        self.analyzer = spot_AM_Report_analyzer()

    def __new__(cls,*args,**kwd):  
        if spot_returner.instance is None:  
            spot_returner.instance = object.__new__(cls, *args, **kwd)
        return spot_returner.instance

    @staticmethod
    def get_instance():
        if spot_returner.instance == None:
            spot_returner.mutex.acquire()
            if spot_returner.instance == None:
                spot_returner.instance = spot_returner()
            spot_returner.mutex.release()         
        return spot_returner.instance
    '''
    SINGLETON PATTERN
    '''

    # FUNCTION: initialize(self, string)
    #   call this function when needs to load a new excel table
    # ARGS:
    #   @excel_file_name: the new excel filename
    def initialize(self, excel_file_name):
        self.analyzer = spot_AM_Report_analyzer(excel_file_name)
        self.analyzer.build_whole_table()

    # FUNCTION: calculate(self, string, string)
    #   to convert one row in the excel into a dictionary
    # ARGS:
    #   @attr1: attribute 1
    #   @attr2: attribute 2
    #   @area: the given area
    # RETURN:
    #   return_data: calculation resule
    def calculate(self, attr1, attr2, area=None):
        res = return_data()
        for it in self.analyzer.get_result_array(area):
            tmp_str = it[attr1][attr2]
            tmp_str = tmp_str[0: len(tmp_str) - 1]
            tmp_float = float(tmp_str)
            if tmp_float >= 100.0:
                res.more_than_100 += 1
            elif tmp_float >= 90.0:
                res.between_90_and_100 += 1
            elif tmp_float >= 70.0:
                res.between_70_and_90 += 1
            elif tmp_float >= 50:
                res.between_50_and_70 += 1
            else:
                res.below_50 += 1
            res.total += 1
        return res.to_dict()